package com.cskaoyan.market.util;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

// 周某的工具类
public class ZhouWenUtil {
    // ban掉构造器
    private ZhouWenUtil() {};

    // 方法将Post请求的请求体，转换为一个已有对象
    public static String getRequestBody(HttpServletRequest request) throws IOException {
        // 获取输入流
        ServletInputStream inputStream = request.getInputStream();
        // 将数据存储至byteArrayOutputStream中
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1){
            byteArrayOutputStream.write(bytes, 0, length);
        }
        // 数据转为utf-8编码并返回
        return byteArrayOutputStream.toString("utf-8");
    }
}
